const Keys = require('../template')

console.log("=== Keys 高级方法测试 ===")

// 测试获取所有键
console.log(
  "获取所有键:", (() => {
    const obj = { a: 1, b: 2 }
    Object.defineProperty(obj, 'c', {
      value: 3,
      enumerable: false
    })

    const keys = Keys.keysAll(obj)

    return Array.isArray(keys) &&
      keys.length === 3 &&
      keys.includes('a') &&
      keys.includes('b') &&
      keys.includes('c')
  })()
)

// 测试深度获取键
console.log(
  "深度获取键:", (() => {
    const obj = { a: { b: { c: 1 } }, d: { e: 2 } }
    const keys = Keys.keysDeep(obj, 2)

    return Array.isArray(keys) &&
      keys.length === 4 &&
      keys.includes('a') &&
      keys.includes('b') &&
      keys.includes('c') &&
      keys.includes('d')
  })()
)

// 测试条件过滤键
console.log(
  "条件过滤键:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const keys = Keys.keysWithFilter(obj, (value, key) => {
      return value > 2
    })

    return Array.isArray(keys) &&
      keys.length === 2 &&
      keys.includes('c') &&
      keys.includes('d') &&
      !keys.includes('a') &&
      !keys.includes('b')
  })()
)

// 测试类型过滤键
console.log(
  "类型过滤键:", (() => {
    const obj = { a: 1, b: 'test', c: true, d: 2 }
    const keys = Keys.keysWithType(obj, 'number')

    return Array.isArray(keys) &&
      keys.length === 2 &&
      keys.includes('a') &&
      keys.includes('d') &&
      !keys.includes('b') &&
      !keys.includes('c')
  })()
)

// 测试正则过滤键
console.log(
  "正则过滤键:", (() => {
    const obj = { user_name: 'John', user_age: 30, admin_id: 1, admin_role: 'admin' }
    const keys = Keys.keysWithPattern(obj, /^user_/)

    return Array.isArray(keys) &&
      keys.length === 2 &&
      keys.includes('user_name') &&
      keys.includes('user_age') &&
      !keys.includes('admin_id') &&
      !keys.includes('admin_role')
  })()
)

// 测试排序键
console.log(
  "排序键:", (() => {
    const obj = { c: 3, a: 1, b: 2 }
    const keys = Keys.keysWithSort(obj, (a, b) => a.localeCompare(b))

    return Array.isArray(keys) &&
      keys.length === 3 &&
      keys[0] === 'a' &&
      keys[1] === 'b' &&
      keys[2] === 'c'
  })()
)

// 测试转换键
console.log(
  "转换键:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const keys = Keys.keysWithTransform(obj, key => key.toUpperCase())

    return Array.isArray(keys) &&
      keys.length === 3 &&
      keys.includes('A') &&
      keys.includes('B') &&
      keys.includes('C')
  })()
)

// 测试验证键
console.log(
  "验证键:", (() => {
    const obj = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = {
      name: { type: 'string', required: true },
      age: { type: 'number', min: 0, max: 150 },
      email: { type: 'string', pattern: /@/ }
    }

    try {
      const keys = Keys.keysWithValidation(obj, schema)
      return Array.isArray(keys) &&
        keys.length === 3 &&
        keys.includes('name') &&
        keys.includes('age') &&
        keys.includes('email')
    } catch (error) {
      return false
    }
  })()
)

// 测试异步获取键
console.log(
  "异步获取键:", (() => {
    const obj = { a: 1, b: 'test' }

    return Keys.keysAsync(obj, async (value, key) => {
      await new Promise(resolve => setTimeout(resolve, 1))
      return value > 0
    }).then(keys => {
      return Array.isArray(keys) &&
        keys.length === 1 &&
        keys.includes('a')
    }).catch(() => false)
  })()
)

// 测试并发获取键
console.log(
  "并发获取键:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }

    return Keys.keysConcurrent(obj, async (value, key) => {
      await new Promise(resolve => setTimeout(resolve, 1))
      return value > 2
    }, 3).then(keys => {
      return Array.isArray(keys) &&
        keys.length === 3 &&
        keys.includes('c') &&
        keys.includes('d') &&
        keys.includes('e')
    }).catch(() => false)
  })()
)

// 测试统计获取键
console.log(
  "统计获取键:", (() => {
    const obj = { a: 1, b: { c: 2 }, d: [3, 4] }
    const result = Keys.keysWithStats(obj)

    return typeof result === 'object' &&
      Array.isArray(result.data) &&
      result.data.length === 3 &&
      typeof result.stats === 'object' &&
      result.stats.totalKeys > 0
  })()
)

console.log("=== Keys 高级方法测试完成 ===")
